#include "Main.h"

unsigned int o_MouseX = 0.0f;
unsigned int o_MouseY = 0.0f;

Collision Scene;

GameInput::GameInput(){
	/*
	f_mouse_sensitivity = 0.1f;
	f_walk_velocity = 0.01f;
	o_MouseX=0;
	o_MouseY=0;
	arrCamPosition[X] = -3.0f;
	arrCamPosition[Y] = -10.0f;
	arrCamPosition[Z] = -8.0f;
	*/


}

void GameInput::MovePlayer(Vector &DeltaVelocity){
	Vector Center,Radius,NewCenter,NewVelocity,InvertedVelocity;

	//Refresh Virtual Player Coordinates
	/*arrVirtualPlayerCoord[0] = arrCamPosition[0];
	arrVirtualPlayerCoord[1] = arrCamPosition[1];
	arrVirtualPlayerCoord[2] = arrCamPosition[2];

	InvertedVelocity.x = DeltaVelocity.x;
	InvertedVelocity.y = DeltaVelocity.y;
	InvertedVelocity.z = DeltaVelocity.z;

	Center.x = arrVirtualPlayerCoord[0];
	Center.y = arrVirtualPlayerCoord[1];
	Center.z = arrVirtualPlayerCoord[2];

	Radius.x = 1.0f;
	Radius.y = 1.0f;
	Radius.z = 1.0f;*/
	
	//Scene.CollideEllipsoid(Center,Radius,DeltaVelocity,NewCenter,NewVelocity);
	//if(InvertedVelocity.x != 0 || InvertedVelocity.y != 0 ||InvertedVelocity.z != 0){
	//Scene.collide_and_slide(Center, Radius,InvertedVelocity,false);
	
	//arrCamPosition[0] += InvertedVelocity.x;
	//arrCamPosition[1] += InvertedVelocity.y;
	//arrCamPosition[2] += InvertedVelocity.z;
	//Scene.collide_and_slide(Center, Radius,InvertedVelocity,true);
	//}
	arrCamPosition[0] += DeltaVelocity.x;
	arrCamPosition[1] += DeltaVelocity.y;
	arrCamPosition[2] += DeltaVelocity.z;



	//arrCamPosition[0] += InvertedVelocity.x;
	//arrCamPosition[1] += InvertedVelocity.y;
	//arrCamPosition[2] += InvertedVelocity.z;
}


GameInput::~GameInput(){

}

void GameInput::GetInputs(){

	int delta_MouseX = 0;
	int delta_MouseY = 0;
	const sf::Input& Input = App.GetInput();
	unsigned int MouseX = Input.GetMouseX();
	unsigned int MouseY = Input.GetMouseY();
	if(MouseX == 0 && MouseY == 0){
		MouseX=320;
		MouseY=240;
	}

	//reset degree
	if(arrViewAngles[YAW] > 360) arrViewAngles[YAW]-=360;
	if(arrViewAngles[YAW] <0) arrViewAngles[YAW] +=360;

	delta_MouseX = 320-MouseX;
	delta_MouseY = 240-MouseY;


	char buffer[6];
	char strCamCoordinates[255];
	strcpy(strCamCoordinates, "(");
	itoa(arrVirtualPlayerCoord[0],buffer,10);
	strcat(strCamCoordinates, buffer);
	itoa(arrVirtualPlayerCoord[2],buffer,10);
	strcat(strCamCoordinates, ",");
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, ")");
	strcat(strCamCoordinates, " Height: ");
	itoa(arrVirtualPlayerCoord[1],buffer,10);
	strcat(strCamCoordinates, buffer);
	itoa(arrViewAngles[YAW],buffer,10);
	strcat(strCamCoordinates, ":");
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, " deg");
	strcat(strCamCoordinates, "\nSphere Position:");


	float spherePosition_x = arrVirtualPlayerCoord[0]-(10.0f*sin((arrViewAngles[YAW])*(PI/180)));
	float spherePosition_y = 0.0f;
	float spherePosition_z = arrVirtualPlayerCoord[2]+(10.0f*cos((arrViewAngles[YAW])*(PI/180)));

	itoa(spherePosition_x,buffer,10);
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, ",");
	itoa(spherePosition_y,buffer,10);
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, ",");
	itoa(spherePosition_z,buffer,10);
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, "\n");
	Text.SetText(strCamCoordinates);

	
	o_MouseX = MouseX;
	o_MouseY = MouseY;
	Vector VelocityVector;
	VelocityVector.x = 0;
	VelocityVector.y = 0;
	VelocityVector.z = 0;
	if(Input.IsKeyDown(sf::Key::Escape)){
		App.Close();
	}
	if(Input.IsKeyDown(sf::Key::W)){
		VelocityVector.x -= f_walk_velocity*sin((arrViewAngles[YAW])*(PI/180));
		VelocityVector.z -= f_walk_velocity*cos((arrViewAngles[YAW])*(PI/180));
	}
	if(Input.IsKeyDown(sf::Key::S)){
		VelocityVector.x -= f_walk_velocity*sin((180+arrViewAngles[YAW])*(PI/180));
		VelocityVector.z -= f_walk_velocity*cos((180+arrViewAngles[YAW])*(PI/180));
	}
	if(Input.IsKeyDown(sf::Key::A)){
		VelocityVector.x -= f_walk_velocity*sin((90+arrViewAngles[YAW])*(PI/180));
		VelocityVector.z -= f_walk_velocity*cos((90+arrViewAngles[YAW])*(PI/180));
	}
	if(Input.IsKeyDown(sf::Key::D)){
		VelocityVector.x -= f_walk_velocity*sin((270+arrViewAngles[YAW])*(PI/180));
		VelocityVector.z -= f_walk_velocity*cos((270+arrViewAngles[YAW])*(PI/180));
	}


	//bool bCollided= Collision::CollideEllipsoid(Center,VelocityVector);
	Vector NewVelocity;
	NewVelocity.x = 0;
	NewVelocity.y = 0.0f;
	NewVelocity.z = 0;
	MovePlayer(VelocityVector);


	//Scene.CollideEllipsoid(Center,Radius,VelocityVector, NewCenter, NewVelocity);
	//arrCamPosition[X] += NewVelocity.x;
	//arrCamPosition[Y] += NewVelocity.y;
	//arrCamPosition[Z] += NewVelocity.z;
	
	//Gravity
	//VelocityVector.x = 0.0f;
	//VelocityVector.y = 0.01f;
	//VelocityVector.z = 0.0f;
	//MovePlayer(VelocityVector);
	//if(!Scene.CollideEllipsoid(Center,Radius,VelocityVector, NewCenter, NewVelocity)) arrCamPosition[Y] += VelocityVector.y;

	if(Input.IsKeyDown(sf::Key::Up)){
		arrViewAngles[PITCH]-=0.1f;
	}
	if(Input.IsKeyDown(sf::Key::Down)){
		arrViewAngles[PITCH]+=0.1f;
	}
	if(Input.IsKeyDown(sf::Key::Left)){
		arrViewAngles[YAW]-=0.1f;
	}
		if(Input.IsKeyDown(sf::Key::Right)){
		arrViewAngles[YAW]+=0.1f;
	}
	
	if(delta_MouseX!=0){
		arrViewAngles[YAW] += delta_MouseX*f_mouse_sensitivity;
	}
	if(delta_MouseY!=0){
		arrViewAngles[PITCH] -= delta_MouseY*f_mouse_sensitivity;
	}
	
}

